/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import com.octo.captcha.service.CaptchaServiceException;
import entity.PersonService;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.CaptchaServiceSingleton;

/**
 *
 * @author Sum
 */
public class ChangeInfoServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String szBirthYear = request.getParameter("birth_year");
        String szBirthMonth = request.getParameter("birth_month");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String captchaCode = request.getParameter("captchacode");
        boolean isCorrectCaptcha = false;
        boolean ok = false;

        HttpSession session = request.getSession(true);
        try {
            isCorrectCaptcha = CaptchaServiceSingleton.getInstance().validateResponseForID(session.getId(), captchaCode);
        } catch (CaptchaServiceException e) {
            //should not happen, may be thrown if the id is not valid
        }

        //todo: check for the same email.
        //todo: check for account activation, resend comfirmation letter if necessary

        if (!isCorrectCaptcha) {
            this.getServletContext().getRequestDispatcher("/changeInfo.jsp?msg=1").forward(request, response);
            //response.sendRedirect("changeInfo.jsp?msg=1");
            return;
        }

        Integer birth_year = null;
        Integer birth_month = null;
        try {
            birth_year = Integer.valueOf(szBirthYear);
            birth_month = Integer.valueOf(szBirthMonth);
        } catch (Exception e) {
        }
        if (birth_year == null || birth_month == null) {
            this.getServletContext().getRequestDispatcher("/changeInfo.jsp?msg=3").forward(request, response);
            return;
        } else {
            Integer id = (Integer) session.getAttribute("personID");
            boolean creationSucceed = PersonService.updateInformation(id, birth_year,birth_month, address, email);
            if (creationSucceed) {
                //TODO:create the MD5 hash and take the first 10 characters as activation code
                // Using the MD5Hash                

                response.sendRedirect("UpdateInfoSuccess.jsp");
                return;
            } else {
                response.sendRedirect("changeInfo.jsp?msg=3");
                return;
            }

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
